package controller

import (
	"mumu_stock_trad/model"
)

// 判断某个路由是否有权限
func hasPermission(route model.Route, roles []string) bool {
	if route.Meta != nil && len(route.Meta.Roles) > 0 {
		for _, r := range roles {
			for _, role := range route.Meta.Roles {
				if r == role {
					return true
				}
			}
		}
		return false
	}
	return true // 没设置角色限制的路由默认允许
}

// 递归过滤整个路由列表
func FilterRoutes(routes []model.Route, roles []string) []model.Route {
	var res []model.Route
	for _, route := range routes {
		if hasPermission(route, roles) {
			newRoute := route
			if len(route.Children) > 0 {
				newRoute.Children = FilterRoutes(route.Children, roles)
			}
			res = append(res, newRoute)
		}
	}
	return res
}
